﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Configuration;
using Sendz.Domain;
using System.Text;

namespace Sendz.WebUI.Helpers
{
    public class EmailHelpers
    {

        public bool SendInvite(User user, Guid inviteeKey, string name, string email, string message)
        {
            name = (!string.IsNullOrWhiteSpace(name)) ? " " + name : string.Empty;
            message = (!string.IsNullOrWhiteSpace(message)) ? "<br /><br />PS...<br /><br />" + message.Replace("\r\n", "<br />").Replace("\n", "<br />").Replace("\r", "<br />").Replace(Environment.NewLine, "<br />") : string.Empty;
            if (SendEmail(ConfigurationManager.AppSettings.Get("FromAddressMarketing"), email,
                                user.Firstname + " " + user.Lastname + " wants you to try Frontdesk",
                                "Hey" + name + ",<br /><br />" + "Come check out Frontdesk. It's a new web application that is designed to let you collect data, reply to people automatically (with custom responses), and learn what people are interested in. It helps you turn leads into customers. I'm already using it...<br /><br />http://" + ConfigurationManager.AppSettings.Get("AppDomain") + ConfigurationManager.AppSettings.Get("AppRootPath") + "/Invite/Welcome/" + inviteeKey + "<br /><br />I hope you enjoy it,<br />" + user.Firstname + message, null, true))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Sends an email using the values provided
        /// </summary>
        /// <param name="emailFrom">Email From</param>
        /// <param name="emailTo">Email To</param>
        /// <param name="emailSubject">Email Subject</param>
        /// <param name="emailBody">Email Body</param>
        /// <returns>Boolean indicating whether the email was sent successfully</returns>
        public bool SendEmail(string emailFrom, string emailTo, string emailSubject, string emailBody, IEnumerable<string> attachments = null, bool sendToAdmin = false)
        {
            var smtp = new SmtpClient(ConfigurationManager.AppSettings.Get("SmtpServer"));
            var requiresLogin = Convert.ToBoolean(ConfigurationManager.AppSettings.Get("SmtpServerRequiresLogin"));
            if (requiresLogin)
            {
                smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings.Get("SmtpUsername"), ConfigurationManager.AppSettings.Get("SmtpPassword"));
            }
            var email = new MailMessage(emailFrom, emailTo, emailSubject, emailBody) { IsBodyHtml = true, BodyEncoding = Encoding.UTF8 };
            var adminEmail = new MailMessage(emailFrom, ConfigurationManager.AppSettings.Get("AdminInbox"), emailSubject, emailBody + "<br /><br />-------------------------------------------<br /><br />Originally sent to: " + emailTo + "<br /><br />(Any attachments are omitted)") { IsBodyHtml = true, BodyEncoding = Encoding.UTF8 };
            // Add attachments if the paths were passed along
            if (attachments != null)
            {
                foreach (var attachment in attachments)
                {
                    try
                    {
                        email.Attachments.Add(new System.Net.Mail.Attachment(attachment));
                    }
                    catch
                    {
                        // Attachment failed
                    }
                }
            }
            try
            {
                smtp.Send(email);
                if (sendToAdmin)
                    smtp.Send(adminEmail);
            }
            catch
            {
                return false;
            }
            finally
            {
                smtp.Dispose();
            }
            return true;
        }
    }
}